package com.bsj.power.common.config.cached;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.bsj.JedisClusterBSJ;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author yinhao
 * @version 1.0
 * @description redis缓存
 * @date 2024/4/17
 */
@Slf4j
@Component
public class RedisCached {

    @Autowired
    private JedisClusterBSJ jedisClusterBSJ;

    public <T> boolean set(String key, T value) {
        return set(key, JSON.toJSONString(value), 0);
    }

    public boolean set(String key, String value, int expireTime) {
        jedisClusterBSJ.set(key, value);
        if (expireTime > 0) {
            jedisClusterBSJ.expire(key, expireTime);
        }
        return true;
    }

    public <T> T get(String key, Class<T> clazz) {
        return JSONObject.parseObject(get(key), clazz);
    }

    public String get(String key) {
        return jedisClusterBSJ.get(key);
    }

    public boolean del(String key) {
        return jedisClusterBSJ.del(key) > 0;
    }

    public void hset(String key, String fileId, String value) {
        jedisClusterBSJ.hset(key, fileId, value);
    }

    public String hget(String key, String fileId) {
        return jedisClusterBSJ.hget(key, fileId);
    }

    public Map<String, String> hgetAll(String key) {
        return jedisClusterBSJ.hgetAll(key);
    }

    public boolean hdel(String key, String fileId) {
        return jedisClusterBSJ.hdel(key, fileId) > 0;
    }

    public boolean exists(String key) {
        return jedisClusterBSJ.exists(key);
    }

    public Long sadd(String key, String value) {
        return jedisClusterBSJ.sadd(key, value);
    }

    public Long srem(String key, String value) {
        return jedisClusterBSJ.srem(key, value);
    }

    public Set<String> smembers(String key) {
        return jedisClusterBSJ.smembers(key);
    }

    public int ttl(String key) {
        return jedisClusterBSJ.ttl(key).intValue();
    }

    public long lpush(String key, String value) {
        return jedisClusterBSJ.lpush(key, value);
    }

    public List<String> lrange(String key, long start, long end) {
        return jedisClusterBSJ.lrange(key, start, end);
    }
}
